package pers.lt.admin.dao;

import org.springframework.stereotype.Component;
import pers.lt.hibernatebeans.OfficesInfoEntity;
import pers.lt.jdbc.JdbcTemplate;
import pers.lt.tools.BeanUtil;
import pers.lt.tools.StringFacs;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * Created by 18621 on 2016/7/5.
 */
@Component("OfficesDaoImp")
public class OfficesDaoImp {
    @Resource
    private JdbcTemplate jdbcTemplate;
     
    //查询权限
    public OfficesInfoEntity selectOffices(OfficesInfoEntity officesInfoEntity){
        OfficesInfoEntity a=new OfficesInfoEntity();
        a=jdbcTemplate.get(OfficesInfoEntity.class, officesInfoEntity.getId());
        return a;
    }
    //保存权限
    public OfficesInfoEntity addOffices(OfficesInfoEntity officesInfoEntity){
        OfficesInfoEntity a=new OfficesInfoEntity();
        a=jdbcTemplate.get(OfficesInfoEntity.class, officesInfoEntity.getId());
        if(StringFacs.isEmpty(a)){
            a=officesInfoEntity;
        }else{
            BeanUtil.copyProperties(officesInfoEntity, a);
        }
        jdbcTemplate.saveOrUpdate(a);
        return a;
    }
    public void officesDelete(OfficesInfoEntity officesInfoEntity){
        jdbcTemplate.delete(officesInfoEntity);
    }
    //权限列表
    public List<Map> officesList(OfficesInfoEntity officesInfoEntity){
        StringBuffer sql = new StringBuffer();
        //select t.* ,（select count(*)  from offices_info where pid = t.id）children from offices_info t
        //查出权限，及权限的子权限个数
        //    select t.* ,aaa.zs child from  officesinfo t left join (select pid,count(*) zs from officesinfo group by  pid) aaa on aaa.pid = t.id
        sql.append("select t.* ,aaa.zs child from  officesinfo t left join (select pid,count(*) zs from officesinfo group by  pid) aaa on aaa.pid = t.id where 1=1 ");
        //sql.append("select t.* ,（select count(*)  from officesinfo where pid = t.id）child from officesinfo t where 1=1 ");
        if(!StringFacs.isEmpty(officesInfoEntity.getPid())) sql.append(" and t.pid = '").append(officesInfoEntity.getPid()).append("'");
        //如果Pid为空，则查出权限的最终父节点
        else sql.append(" and t.pid is null ");
        if(!StringFacs.isEmpty(officesInfoEntity.getOfficesName()))  sql.append(" and t.offices_name like '%").append(officesInfoEntity.getOfficesName()).append("%'");
        return jdbcTemplate.queryDatas(sql.toString());
    }
    
}
